package cn.xiaye.blog.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.xiaye.blog.bean.Sort;
import cn.xiaye.blog.db.util.DbUtil;

public class SortDao {
	private Sort sort = new Sort();
	DbUtil dbUtil = DbUtil.getDefaultDatabaseUtil();

	public void insertSort() {
		PreparedStatement ps = null;
		try {
			ps = dbUtil.prepareStatement("insert into sort values (null,?);");
			ps.setString(1, sort.getName());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbUtil.close(ps);
		}
	}

	public void updateSort() {
		PreparedStatement ps = null;
		try {
			ps = dbUtil.prepareStatement("");
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public List<Sort> queryAll() {
		List<Sort> sortLst = new ArrayList<Sort>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = dbUtil.prepareStatement("select * from sort");
			rs = ps.executeQuery();
			while(rs.next()){
				Sort sort = new Sort();
				sort.setId(rs.getInt("id"));
				sort.setName(rs.getString("name"));
				sortLst.add(sort);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			dbUtil.close(ps);
			dbUtil.close(rs);
		}
		return sortLst;
	}

	public void deleteSort() {
		PreparedStatement ps = null;
		try {
			ps = dbUtil.prepareStatement("");
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
